package com.sskj.market.ui.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.Group;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.sskj.common.base.App;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppCircleCheckUtil;
import com.sskj.market.R;
import com.sskj.market.R2;
import com.sskj.market.bean.AddressBean;
import com.sskj.market.bean.CreateOrderBean;
import com.sskj.market.bean.DefaultAddressBean;
import com.sskj.market.bean.OrderBuyBean;
import com.sskj.market.component.DaggerUserDataComponent;
import com.sskj.market.http.HttpConfig;
import com.sskj.market.presenter.SubmitOrderPresenter;
import com.sskj.market.util.GlideUtils;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * @author budou
 */
public class SubmitOrderActivity extends BaseActivity<SubmitOrderPresenter> {
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.ivBack)
    LinearLayout ivBack;
    @BindView(R2.id.tvReturn)
    TextView tvBack;
    @BindView(R2.id.tvRight)
    TextView tvRight;
    @BindView(R2.id.ivRight)
    ImageView ivRight;
    @BindView(R2.id.view_address_order_submit_market)
    View viewAddressOrderSubmitMarket;
    @BindView(R2.id.group_address_tip_order_market)
    Group groupAddressTipOrderMarket;
    @BindView(R2.id.iv_map_order_submit_market)
    ImageView ivMapOrderSubmitMarket;
    @BindView(R2.id.tv_tips_order_submit_market)
    TextView tvTipsOrderSubmitMarket;
    @BindView(R2.id.group_address_info_order_market)
    Group groupAddressInfoOrderMarket;
    @BindView(R2.id.iv_item_map_order_submit_market)
    ImageView ivItemMapOrderSubmitMarket;
    @BindView(R2.id.tv_name_address_order_submit_market)
    TextView tvNameAddressOrderSubmitMarket;
    @BindView(R2.id.tv_phone_address_order_submit_market)
    TextView tvPhoneAddressOrderSubmitMarket;
    @BindView(R2.id.tv_info_address_order_submit_market)
    TextView tvInfoAddressOrderSubmitMarket;
    @BindView(R2.id.tv_type_order_submit_market)
    TextView tvTypeOrderSubmitMarket;
    @BindView(R2.id.view_goods_order_submit_market)
    View viewGoodsOrderSubmitMarket;
    @BindView(R2.id.tv_stores_name_order_submit_market)
    TextView tvStoresNameOrderSubmitMarket;
    @BindView(R2.id.iv_goods_name_order_submit_market)
    ImageView ivGoodsNameOrderSubmitMarket;
    @BindView(R2.id.tv_goods_name_order_submit_market)
    TextView tvGoodsNameOrderSubmitMarket;
    @BindView(R2.id.tv_goods_price_order_submit_market)
    TextView tvGoodsPriceOrderSubmitMarket;
    @BindView(R2.id.iv_goods_count_order_submit_market)
    TextView ivGoodsCountOrderSubmitMarket;
    @BindView(R2.id.view_goods_info_order_submit_market)
    View viewGoodsInfoOrderSubmitMarket;
    @BindView(R2.id.tv_goods_express_price_title_order_submit_market)
    TextView tvGoodsExpressPriceTitleOrderSubmitMarket;
    @BindView(R2.id.tv_goods_remark_title_order_submit_market)
    TextView tvGoodsRemarkTitleOrderSubmitMarket;
    @BindView(R2.id.tv_goods_express_price_order_submit_market)
    TextView tvGoodsExpressPriceOrderSubmitMarket;
    @BindView(R2.id.et_goods_remark_order_submit_market)
    EditText etGoodsRemarkOrderSubmitMarket;
    @BindView(R2.id.tv_goods_all_price_order_submit_market)
    TextView tvGoodsAllPriceOrderSubmitMarket;
    @BindView(R2.id.tv_goods_all_count_order_submit_market)
    TextView tvGoodsAllCountOrderSubmitMarket;
    @BindView(R2.id.btn_ensure_order_submit_market)
    Button btnEnsureOrderSubmitMarket;

    private String areaId;
    private String produceId, num;

    @Inject
    UserViewModel userViewModel;
    UserData userData;

    @Override
    protected int getLayoutId() {
        return R.layout.market_activity_submit_order_main;
    }

    @Override
    public SubmitOrderPresenter getPresenter() {
        return new SubmitOrderPresenter();
    }


    @SuppressLint("SetTextI18n")
    @Override
    protected void initView() {
        super.initView();
        DaggerUserDataComponent.create().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
        });


        setTitle(App.INSTANCE.getString(R.string.market_submitOrderActivity1));
        tvBack.setVisibility(View.VISIBLE);
        Bundle bundle = getIntent().getExtras();
        assert bundle != null;
        OrderBuyBean bean = (OrderBuyBean) bundle.getSerializable("goodsInfo");
        assert bean != null;
        GlideUtils.Show(bean.getGoodsImg(),ivGoodsNameOrderSubmitMarket);
        tvGoodsNameOrderSubmitMarket.setText(bean.getGoodsName());
        tvGoodsPriceOrderSubmitMarket.setText(bean.getGoodsPrice());
        ivGoodsCountOrderSubmitMarket.setText("×" + bean.getAllCount());
        tvGoodsAllCountOrderSubmitMarket.setText(App.INSTANCE.getString(R.string.market_submitOrderActivity2) + bean.getAllCount() + App.INSTANCE.getString(R.string.market_submitOrderActivity3));
        tvGoodsAllPriceOrderSubmitMarket.setText(App.INSTANCE.getString(R.string.market_submitOrderActivity4) + bean.getAllPrice() + "LAC");
        tvGoodsExpressPriceOrderSubmitMarket.setText(bean.getExpressPrice() + "LAC");
        produceId = bean.getGoodsId();
        num = bean.getAllCount();
        RxBus.getDefault().register(this);

    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.listDefaultAddress(mPresenter);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        ClickUtil.click(viewAddressOrderSubmitMarket, () -> {
            if (AppCircleCheckUtil.checkLogin(this, userData)) {
                ARouter.getInstance().build(RConfig.MARKET_My_ADDRESS).navigation();
            }
        });
        ClickUtil.click(btnEnsureOrderSubmitMarket, () -> {
            if (!AppCircleCheckUtil.checkLogin(this, userData)) {
                return;
            }
            if (TextUtils.isEmpty(areaId)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.market_submitOrderActivity5));
                return;
            }
            mPresenter.createOrder(produceId, areaId, num, etGoodsRemarkOrderSubmitMarket.getText().toString(), mPresenter);
        });


    }


    /**
     * 获取默认地址成功
     */
    @SuppressLint("SetTextI18n")
    public void getDefaultAddressSuccess(DefaultAddressBean bean) {
        //提示地址不展示
        groupAddressTipOrderMarket.setVisibility(View.GONE);
        //地址信息显示
        groupAddressInfoOrderMarket.setVisibility(View.VISIBLE);
        areaId = bean.getData().getId() + "";
        tvNameAddressOrderSubmitMarket.setText(bean.getData().getName());
        tvPhoneAddressOrderSubmitMarket.setText(bean.getData().getPhone());
        tvInfoAddressOrderSubmitMarket.setText(bean.getData().getProvince() + "  " + bean.getData().getCity() + "  "
                + bean.getData().getCountry() + "  " + bean.getData().getDetail());

    }

    /**
     * 获取默认地址失败
     */
    public void getDefaultAddressFailed(String msg) {
        //提示地址展示
        groupAddressTipOrderMarket.setVisibility(View.VISIBLE);
        //地址信息不显示
        groupAddressInfoOrderMarket.setVisibility(View.GONE);
    }


    public void createOrderSuccess(CreateOrderBean bean) {
        //到达订单详情
        Bundle bundle = new Bundle();
        bundle.putInt("orderId", bean.getData().getId());
        Intent intent = new Intent(this, OrderDetailsActivity.class);
        intent.putExtras(bundle);
        startActivity(intent);
        finish();
    }


    /**
     * 地址点击返回选中地址信息
     *
     * @param bean 地址信息
     */
    @SuppressLint("SetTextI18n")
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getAddressInfo(AddressBean bean) {
        groupAddressTipOrderMarket.setVisibility(View.GONE);
        groupAddressInfoOrderMarket.setVisibility(View.VISIBLE);
        areaId = bean.getId() + "";
        tvNameAddressOrderSubmitMarket.setText(bean.getName());
        tvPhoneAddressOrderSubmitMarket.setText(bean.getPhone());
        tvInfoAddressOrderSubmitMarket.setText(bean.getProvince() + "  " + bean.getCity() + "  " + bean.getCountry() + "  " + bean.getDetail());
    }


}
